//
//  LinkingCardInfoCell.swift
//  iPhone
//
//  Created by Toj on 11/9/22.
//

import UIKit

class LinkingCardInfoCell: UIView {
    
    var lineType: TTTextLineType = .enter { willSet {
        if lineType == newValue { return }
        setNeedsLayout()
    } }
    
    var title: String = "" { didSet {
        titleLabel.text = title
    } }
    
    var text: String = "" { didSet {
        textLabel.text = text
    } }
    
    var image: UIImage? = nil { didSet {
        imageView.image = image
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupAddSubviews()
        backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayoutSubviews()
    }
    
    private let imageViewHeight = 40.ui_scale
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .center
        view.backgroundColor = "#FFEEC0".to_color
        view.cm_cornerRadius = imageViewHeight * 0.5
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#666666".to_color
        view.font = UIFont.systemFont(ofSize: 12.f_scale)
        return view
    }()
    
    private lazy var textLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#666666".to_color
        view.font = UIFont.boldSystemFont(ofSize: 12.f_scale)
        return view
    }()
    
    deinit {
        // print("-->toj: LinkingCardInfoCell " + #function)
    }
}

extension LinkingCardInfoCell {
    
    private func setupAddSubviews() {
        
        addSubview(imageView)
        addSubview(titleLabel)
        addSubview(textLabel)
    }
    
    private func setupLayoutSubviews() {
        imageView.snp.makeConstraints { make in
            make.centerY.equalTo(self)
            make.left.equalTo(6.ui_scale)
            make.height.width.equalTo(imageViewHeight)
        }
        
        if lineType == .enter {
            titleLabel.snp.remakeConstraints { make in
                make.top.equalTo(10)
                make.left.equalTo(imageView.snp.right).offset(8.ui_scale)
            }
            
            textLabel.snp.remakeConstraints { make in
                make.bottom.equalTo(snp.bottom).offset(-10)
                make.left.equalTo(imageView.snp.right).offset(8.ui_scale)
            }
            return
        }
        
        titleLabel.snp.remakeConstraints { make in
            make.left.equalTo(imageView.snp.right).offset(8.ui_scale)
            make.centerY.equalTo(imageView)
        }
        
        textLabel.snp.remakeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(2.ui_scale)
            make.centerY.equalTo(imageView)
        }
    }
}

